<template>

  <el-container class="home-container">
    <!-- 页面头部区域 -->
    <el-header>
      <div>
        <img src="../assets/logo.png" alt="">
        <span>电商管理后台</span>
      </div>

      <div>
        <i class="iconfont icon-morentouxiang"></i>
        <span>{{uname}}</span>
        <el-button @click='logout' size="medium">退出</el-button>
        <!-- <el-link type="warning">信息链接</el-link> -->
      </div>
    </el-header>
    <!-- 页面主体区域 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="isCollapse?'64px':'200px'">
        <!-- 侧边栏缩起图标 -->
        <div class="taggle-button" @click='taggleCollapse'>
          <i :class="isCollapse?'el-icon-s-unfold':'el-icon-s-fold'"></i>
        </div>
        <!-- 侧边栏菜单，开启路由模式 -->
        <el-menu :default-active='activePath' router :collapse-transition="false" :collapse="isCollapse" unique-opened background-color="#313743" text-color="#fff" active-text-color="#4F9FF6">
          <!-- 一级菜单 -->
          <el-submenu :index="item.id+''" v-for="(item) in menuList" :key='item.id'>
            <!-- 一级菜单模板区域 -->
            <template slot="title">
              <!-- 图标 -->
              <i :class="iconObj[item.id]"></i>
              <!-- 文本 -->
              <span>{{item.authName}}</span>
            </template>

            <!-- 二级菜单，index的值为跳转地址 -->
            <el-menu-item :index="'/'+subItem.path" v-for="subItem in item.children" :key="subItem.id" @click="saveNavState('/'+subItem.path)">
              <!-- 二级菜单模板区域 -->
              <template slot="title">
                <!-- 图标 -->
                <i class="el-icon-menu"></i>
                <!-- 文本 -->
                <span>{{subItem.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>

        </el-menu>
      </el-aside>
      <!-- 右侧内容主体 -->
      <el-main>
        <!-- welcome组件路由占位符 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>

</template>

<script>
export default {
  data() {
    return {
      uname: '',
      // 左侧菜单数据
      menuList: [],
      iconObj: {
        125: 'iconfont icon-users',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      },
      isCollapse: false,
      // 被激活的链接地址
      activePath: ''
    }
  },
  created() {
    this.getUsername()
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    getUsername() {
      this.uname = window.sessionStorage.getItem('username')
    },
    logout() {
      // 使用清空token退出登录
      // 清空token
      window.sessionStorage.clear()
      // 跳转登录页
      this.$router.push('/login')
    },
    // 获取所有的菜单
    async getMenuList() {
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.menuList = res.data
    },
    taggleCollapse() {
      // 点击按钮切换菜单的折叠与展开
      this.isCollapse = !this.isCollapse
    },
    saveNavState(activePath) {
      // 保存链接的激活状态
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    }
  }

}
</script>

<style lang="less" scoped>
.home-container {
  height: 100%;
}
.el-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background-color: #363d40;
  color: #fff;
  font-size: 20px;
  > div {
    display: flex;
    align-items: center;
    > img {
      width: 50px;
      height: 50px;
      border-radius: 50%;
    }
    > span {
      margin: 0 15px;
    }
    > i {
      margin-right: 0;
      font-size: 35px;
    }
  }
}
.el-aside {
  background-color: #313743;
  transition: all 0.2s;
}
.el-main {
  background-color: #e9edf1;
}
.iconfont {
  margin-right: 10px;
}
.el-menu {
  border-right: none;
}
.taggle-button {
  background-color: #4a5064;
  color: #e2e2e2;
  font-size: 18px;
  line-height: 24px;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;
}
</style>
